package io;

import org.apache.commons.io.IOUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.support.EncodedResource;

import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.net.URL;

public class SpringResourceDemo {
    public static void main(String[] args) throws IOException {
       demoClassPathResource("test.log");
        demoClassPathResource("/META-INF/path.log");
        writeableResource();
    }

    /**
     *
     * @param cpPath
     * @throws IOException
     */
    public static void demoClassPathResource(String cpPath) throws IOException {
        ClassPathResource cpResource = new ClassPathResource(cpPath);
        File file = cpResource.getFile();
        String absolutePath = file.getAbsolutePath();
        System.out.println(cpPath+":" + absolutePath);
        URL url = cpResource.getURL();
        System.out.println("url:"+url.toString());
    }

    public static void writeableResource() throws IOException {
        String file = System.getProperty("user.dir")+"\\think-in-spring\\spring-resource\\src\\main\\java\\io\\SpringResourceDemo.java";
        FileSystemResource sfr = new FileSystemResource(file);
        EncodedResource encodedResource = new EncodedResource(sfr,"UTF-8");
        System.out.println(encodedResource.getResource());
        try(Reader reader = encodedResource.getReader()) {
            System.out.println(IOUtils.toString(reader));
        }
    }


}
